package com.school.view;

import com.school.domain.Admin;
import com.school.domain.Student;
import com.school.domain.Teacher;
import com.school.domain.TextBook;
import com.school.proxy.MyProxy;
import com.school.service.AdminService;

import java.util.List;
import java.util.Scanner;

public class AdminView {
    private long aid;
    private final AdminService adminService = (AdminService) MyProxy.getProxy(AdminService.class, "127.0.0.1", 10000);

    Scanner scanner = new Scanner(System.in);

    public AdminView() {
    }

    public void adminView() {
        Scanner scanner = new Scanner(System.in);
        login();
        boolean flag = true;
        while (flag) {
            System.out.println("1.查看个人信息\t2.修改个人信息\t3.添加学生\t4.添加老师\t5.添加教材\t6.重置学生密码\t7.修改学生信息\t8.修改教师信息\t9.查询所有同学\t10.查询所有教师\t11.登出");
            int chose = scanner.nextInt();
            switch (chose) {
                case 1:
                    showInfo();
                    break;
                case 2:
                    changeInfo();
                    break;
                case 3:
                    addStudent();
                    break;
                case 4:
                    addTeacher();
                    break;
                case 5:
                    addTextbook();
                    break;
                case 6:
                    resetPwd();
                    break;
                case 7:
                    changeStudentInfo();
                    break;
                case 8:
                    changeTeacherInfo();
                    break;
                case 9:
                    showAllStudent();
                    break;
                case 10:
                    showAllTeacher();
                    break;
                case 11:
                    flag = false;
                    break;
                default:
                    System.out.println("请输入正确选项！");
                    break;
            }
        }
    }
    public void login() {
        while (true) {
            System.out.println("请输入账户密码：");
            long id = scanner.nextLong();
            String pwd = scanner.next();
            if (adminService.adminLogin(id, pwd)){
                aid = id;
                break;
            }
            System.out.println("账户或密码错误！");
        }

    }
    public void showInfo(){
        System.out.println(adminService.findById(aid));
    }
    public void changeInfo(){
        Admin admin = adminService.findById(aid);
        System.out.print("输入姓名：");
        admin.setName(scanner.next());
        System.out.print("输入密码：");
        admin.setPwd(scanner.next());
        adminService.update(admin);
    }
    public void addStudent(){
        Student student = new Student();
        System.out.print("输入姓名：");
        student.setName(scanner.next());
        System.out.print("输入密码：");
        student.setPwd(scanner.next());
        System.out.print("输入年龄：");
        student.setAge(scanner.nextLong());
        System.out.print("输入性别：");
        student.setSex(scanner.next());
        System.out.print("输入所属班级编号：");
        student.setClid(scanner.nextLong());
        if (adminService.addStudent(student)) {
            System.out.println("添加成功！");
            return;
        }
        System.out.println("添加失败！");
    }
    public void addTeacher(){
        Teacher teacher = new Teacher();
        System.out.print("输入姓名：");
        teacher.setName(scanner.next());
        System.out.print("输入密码：");
        teacher.setPwd(scanner.next());
        System.out.print("输入年龄：");
        teacher.setAge(scanner.nextLong());
        System.out.print("输入性别：");
        teacher.setSex(scanner.next());
        if (adminService.addTeacher(teacher)) {
            System.out.println("添加成功！");
            return;
        }
        System.out.println("添加失败！");
    }
    public void addTextbook(){
        TextBook textBook = new TextBook();
        System.out.print("输入教材名：");
        textBook.setName(scanner.next());
        if (adminService.addTextBook(textBook)) {
            System.out.println("添加成功！");
            return;
        }
        System.out.println("添加失败！");
    }
    public void resetPwd(){
        System.out.println("输入重置密码：");
        adminService.updateStudentPwd(scanner.next());
    }

    public void changeStudentInfo() {
        Student student;
        while (true) {
            System.out.println("输入学生编号：");
            student = adminService.findStudent(scanner.nextLong());
            if (student != null) break;
            System.out.println("无此学生！");
        }
        System.out.println("学生信息：");
        System.out.println(student);
        System.out.println("-------------------");
        System.out.print("输入姓名：");
        student.setName(scanner.next());
        System.out.print("输入密码：");
        student.setPwd(scanner.next());
        System.out.print("输入年龄：");
        student.setAge(scanner.nextLong());
        System.out.print("输入性别：");
        student.setSex(scanner.next());
        adminService.updateStudent(student);
    }

    public void changeTeacherInfo() {
        Teacher teacher;
        while (true) {
            System.out.println("输入教师编号：");
            teacher = adminService.findTeacher(scanner.nextLong());
            if (teacher != null) break;
            System.out.println("无此教师！");
        }
        System.out.println("教室信息：");
        System.out.println(teacher);
        System.out.println("-------------------");

        System.out.print("输入姓名：");
        teacher.setName(scanner.next());
        System.out.print("输入密码：");
        teacher.setPwd(scanner.next());
        System.out.print("输入年龄：");
        teacher.setAge(scanner.nextLong());
        System.out.print("输入性别：");
        teacher.setSex(scanner.next());
        adminService.updateTeacher(teacher);
    }

    public void showAllStudent() {
        List<Student> allStudent = adminService.findAllStudent();
        for (Student student : allStudent) {
            System.out.println(student);
        }
    }

    public void showAllTeacher() {
        List<Teacher> allTeacher = adminService.findAllTeacher();
        for (Teacher teacher : allTeacher) {
            System.out.println(teacher);
        }
    }




}
